package com.dingding.gmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.gmall.common.dto.MemberPriceDTO;
import com.dingding.gmall.common.dto.SkuRedutionDTO;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.Query;
import com.dingding.gmall.coupon.dao.SkuFullReductionDao;
import com.dingding.gmall.coupon.entity.MemberPriceEntity;
import com.dingding.gmall.coupon.entity.SkuFullReductionEntity;
import com.dingding.gmall.coupon.entity.SkuLadderEntity;
import com.dingding.gmall.coupon.service.MemberPriceService;
import com.dingding.gmall.coupon.service.SkuFullReductionService;
import com.dingding.gmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }


    @Override
    public void saveSkuRedution(SkuRedutionDTO skuRedutionDTO) {
        //1、保存满减，打折
        //1、sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        //满减数量；满减数量的折扣；是否可叠加优惠；折后价
        BeanUtils.copyProperties(skuRedutionDTO, skuLadderEntity);
        skuLadderEntity.setAddOther(skuRedutionDTO.getCountStatus());
//        skuLadderEntity.setPrice();
        if (skuRedutionDTO.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }

        //2、sms_sku_full_redution
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuRedutionDTO, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
            this.save(skuFullReductionEntity);
        }

        //3、sms_member_price
        List<MemberPriceDTO> memberPriceList = skuRedutionDTO.getMemberPrice();
        if (CollectionUtils.isNotEmpty(memberPriceList)) {
            List<MemberPriceEntity> collect = memberPriceList.stream()
                    .map(item -> {
                        MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                        memberPriceEntity.setSkuId(skuRedutionDTO.getSkuId());
                        memberPriceEntity.setMemberLevelId(item.getId());
                        memberPriceEntity.setMemberLevelName(item.getName());
                        memberPriceEntity.setMemberPrice(item.getPrice());
                        memberPriceEntity.setAddOther(1);
                        return memberPriceEntity;
                    })
                    .filter(item -> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1)
                    .collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }
    }
}